#pragma once

#include <iostream>
#include <string>
#include <stdlib.h>
#include <sys/epoll.h>
#include "Log.hpp"
#include "Err.hpp"

static const int defaultEpfd = -1;
static const int gsize = 128;

class Epoller{
public:
    Epoller(): epfd_(defaultEpfd){

    }

    void Create(){
        epfd_ = epoll_create(gsize);
        if(epfd_ < 0){
            logMessage(Fatal, "epoll_create error, code: %d, errstring: %s", errno, strerror(errno));
            exit(EPOLL_CREATE_ERR);
        }
    }

    bool AddModEvent(int fd, uint32_t events, int op){
        struct epoll_event ev;
        ev.events = events;
        ev.data.fd = fd;
        int n = epoll_ctl(epfd_, op, fd, &ev);
        if(n < 0){
            logMessage(Fatal, "epoll_ctl error, code: %d, errstring: %s", errno, strerror(errno));
            return false;
        }
        return true;
    }

    bool DelEvent(int fd){
        return epoll_ctl(epfd_, EPOLL_CTL_DEL, fd, nullptr) == 0;
    }

    int Wait(struct epoll_event* revs, int num, int timeout){
        return epoll_wait(epfd_, revs, num, timeout);
    }

    int GetFd(){
        return epfd_;
    }

    void Close(){
        if(epfd_ != defaultEpfd)
            close(epfd_);
    }

    ~Epoller(){

    }
    
private:
    int epfd_;
};